package org.tea.springdata.jpa.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.tea.springdata.jpa.domain.User;

/**
 * @Author: 张廷
 * @Description: 类描述
 * @Date: 2012-6-29
 * @Time: 下午12:57:06
 */
@Repository("userDao")
public interface IUserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User>{
	
	@Query(value = "SELECT u FROM User u WHERE u.name = ?1")
	public abstract List<User> findUserWithParamIndex(String name);
	
	/**
	 * @MethodDesc :
	 * @param name
	 * @return
	 */
	@Query(value = "SELECT u FROM User u WHERE u.name = :name1")
	public abstract List<User> findUserWithParamName(@Param("name1") String name);
	
	/**
	 * @MethodDesc : 修改用户的姓名,条件为用户的ID
	 * @param id 用户ID
	 * @param name 用户姓名
	 * @return
	 */
	@Modifying
	@Query("UPDATE User u SET u.name = ?2 WHERE u.id = ?1")
	public abstract int updateFixedNameById(Long id, String name);
	
	@Query(value = "SELECT u FROM User u WHERE u.name = :name1",
		   countQuery = "SELECT count(u) FROM User u WHERE u.name = :name1")
	public Page<User> listPageByName(@Param("name1") String name, Pageable pageable);
	
}
